<?php
namespace app\app\controller;

use think\Controller;

class Register extends Controller {
    /*
     * app帐户注册
     */
    public function index() {
        if(request()->isPost()) {
            //获取相关的数据
            $data = input('post.');
            $username = $data['username'];
            $password = $data['password'];  // 已经过md5加密
            $postConfirmPwd = $data['postconfirmpwd'];
            $code = $data['code'];
            
            $ret1 = model('AppUserApply')->where('tel', '=', $username)->order('id', 'DESC')->limit(1)->select();
            $applyTime = strtotime($ret1[0]->create_time) + 6000;
            if($code != $ret1[0]->code) {
                // 验证码不正确
                return json_encode(4104);
            }
            if($applyTime < time()) {
                // 验证码过期
                return json_encode(4105);
            }
            // 验证通过
            if($code == $ret1[0]->code) {
                
                //查找用户名是否存在
                $ret2 = model('AppUser')->get(['username'=>$username]);
                if($ret2) {
                    // 用户名已存在
                    $code = 4101;
                } else {
                    // 用户名可注册
                    if($password === $postConfirmPwd) {
                        // 账户相关的信息检验
                        // 自动生成 密码的加盐字符串
                        $randCode = mt_rand(100, 10000);
                        $appUserData = [
                            'username' => $username,
                            'password' => md5($password . 'changyuannet'),
                            'code' => $randCode,
                        ];
                        $appUserId = model('AppUser')->add($appUserData);

                        // 生成默认的商户信息
                        $imageUrl = '/upload/myphoto.png';  // 生成默认头像
                        $appUserInfoData = [
                            'username' => $username,
                            'bis_name' => $username,
                            'bis_id' => 'BIM' . $username . 'V' . $randCode,
                            'image' => $imageUrl,
                        ];
                        $appUserInfoId = model('AppUserInfo')->add($appUserInfoData);

                        if(!$appUserId && !$appUserInfoId) {
                            // 因写入数据库，导致注册失败
                            $code = 4103;
                        } else {
                            // 注册成功
                            $code = 4100;
                        }
                    } else {
                        // 密码与确认密码不同
                        $code = 4102;
                    }
                }
            }
            return json_encode($code);
        }
    }

    public function test() {
        $ret = model('AppUserApply')->where('tel', '=', $username)->order('id', 'DESC')->limit(1)->select();
//            $ret = model('AppUserApply')->get(function($query){$query->where('tel', '=', '15907539201')->order('id', 'desc');});
        dump(strtotime($ret->create_time));
            
    }
    
}